import React, { useEffect } from 'react';
import { Button } from 'antd';
import { Link, useSelector, useDispatch } from 'umi';

export default () => {
  const dispatch = useDispatch()
  const { currentUser } = useSelector(({ global }) => global)
  useEffect(_ => {
    dispatch({ type: "global/fetchCurrent" })
    document.addEventListener("astilectron-ready", _ => {
      const { dialog } = window.require("electron").remote
      console.log("electron", dialog)
      console.log("window", window)
      window.astilectron.onMessage(msg => {
        console.log("on message ", msg)
        window.astilectron.sendMessage({ name: "set_path", payload: "are u ok?" }, msg => {
          console.log("send result ", msg)
        })
        dialog.showOpenDialogSync({
          properties: ['openFile'],
          title: "选择现代服目录: 魔兽安装目录\\_retail_\\Wow.exe",
          buttonLabel: "确定",
          filters: [{ name: "Wow", extensions: ["exe"] }]
        })
        return { payload: "payload" }
      })
    })
  }, [])
  return (
    <div>
      {currentUser.Id ? <div>
        当前用户: {currentUser.Name}
        <Button onClick={_ => {
          dispatch({ type: "global/logout" })
        }}>退出登录</Button>
      </div> : <Button>
          <Link to="/login">login</Link>
        </Button>}
    </div>
  );
};
